<?php

class FieldController extends \BaseController {

    /**
     * 返回全部字段
     * @return [array] $fields
     * $fields['id']            字段id
     * $fields['name']          字段名
     * $fields['type']          字段类型
     * $fields['displayName']   字段显示名
     */
    public function index(){
        $fields = Field::all();
        return $fields;
    }

    /**
     * Show the form for creating a new resource.
     * GET /field/create
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * 增加字段
     * @param [int]     $modelId    模型id
     * @param [string]  $name       字段名
     * @param [string]  $type       字段类型
     * @param [string]  $displayName字段显示名
     * @return [array] 异常信息
     */
    public function add(){
        $data = Input::all();
        $rules = array(
                'modelId' => 'required|integer|exists:model,id',
                'name'  => 'required||unique:field,name,',
                'type' => 'required',
                'displayName' => 'required|unique:field,display_name,',
            );
        BaseService::validateInputParam($data, $rules);
        $result = FieldService::add($data);
        return $result;
    }

    /**
     * Display the specified resource.
     * GET /field/{id}
     *
     * @param  [int]  $id
     * @return Response
     */
    public function show(){
        $modelId = Input::get('modelId');
        $fields = Field::where('model_id', $modelId)->get();
        return $fields;
    }

    /**
     * Show the form for editing the specified resource.
     * GET /field/{id}/edit
     *
     * @param  int  $id
     * @return Response
     */
    public function edit(){
        
    }

    /**
     * @param  [int]    $id         字段id
     * @param  [string] $name       字段名
     * @param  [string] $type       字段类型
     * @param  [string] $displayName字段显示名
     * @return 异常信息
     */
    public function update(){
        $data = Input::all();
        $rules = array(
                'id' => 'required|integer|exists:field,id',
                'name'  => 'required||unique:field,name,'.$data['id'],
                'type' => 'required',
                'displayName' => 'required|unique:field,display_name,'.$data['id'],
            );
        BaseService::validateInputParam($data, $rules);
        $result = FieldService::update($data);
        return $result;
    }

    /**
     * @param  [int]  $id
     * @return 异常信息
     */
    public function delete(){
        $id = Input::get('id');
        $rules = array('id' => 'required|exists:field,id');
        BaseService::validateInputParam(array('id' => $id), $rules);
        $result = FieldService::delete($id);
        return $result;
    }

    public function test(){

        
    }

}